package com.fwj.user.web.rbac;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import com.fwj.user.annotation.RolePermissions;
import com.fwj.user.api.rbac.RBACService;
import com.fwj.user.entity.rbac.role.RBACRelationUserAndRole;
import com.fwj.user.entity.user.UserBaseInfo;
import com.fwj.user.model.rbac.role.UserAndRoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: fwj
 * @Data: 2022/5/1 14:05
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/RBAC")
public class RBACController {

    //添加日志工厂
    private static final Logger LOGGER = LoggerFactory.getLogger(RBACController.class);
    @Autowired
    private RBACService rbacService;


    /**
     * @Description TODO 为用户绑定角色
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/bindRoleWithUser")
    public JSONObject bindRoleWithUser(@RequestBody JSONObject jsonObject,@LoginUser UserBaseInfo userBaseInfo){
        try {
            UserAndRoleVo userAndRoleVo = JSONObject.parseObject(jsonObject.toJSONString(),UserAndRoleVo.class);
            return rbacService.bindRoleForUser(userAndRoleVo,userBaseInfo);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    /**
     * @Description TODO 查询用户具有的角色列表
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryRoleListByUser")
    public JSONObject queryRoleListByUser(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            UserBaseInfo execUser = JSONObject.parseObject(jsonObject.toJSONString(),UserBaseInfo.class);
            return rbacService.queryRoleListByUser(execUser);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    /**
     * @Description TODO 查询用户和权限是否关联
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryRelationUserAndAuth")
    public JSONObject queryRelationUserAndAuth(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            Long userNumber = Long.parseLong(jsonObject.getString("userNumber"));
            String authNumber = jsonObject.getString("authNumber");
            return rbacService.queryRelationUserAndAuth(userNumber,authNumber);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    /**
     * @Description TODO 查询用户和角色是否关联
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/queryRelationUserAndRole")
    @RolePermissions(Roles = {"20220501224280064"})
    public JSONObject queryRelationUserAndRole(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            UserAndRoleVo userAndRoleVo = JSONObject.parseObject(jsonObject.toJSONString(),UserAndRoleVo.class);
            return rbacService.queryRelationUserAndRole(userAndRoleVo);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    /**
     * @Description TODO 为用户解绑角色
     * @Param: jsonObject
     * @Author fwj
     * @Createtime 2022/5/1
     */
    @PostMapping("/unBindRoleWithUser")
    public JSONObject unBindRoleWithUser(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            UserAndRoleVo userAndRoleVo = JSONObject.parseObject(jsonObject.toJSONString(),UserAndRoleVo.class);
            return rbacService.unBindRoleWithUser(userAndRoleVo);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return new ResponseDTO<>().fail(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

}
